package com.sjme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjme.Feign.CandidateClient;
import com.sjme.Feign.UserClient;
import com.sjme.domain.interview.CandidateInterviewer;
import com.sjme.domain.candidate.Candidate;
import com.sjme.domain.interview.InterviewEvaluation;
import com.sjme.domain.interview.dto.InterviewEvaluationDto;
import com.sjme.domain.interview.vo.InterviewEvaluationVo;
import com.sjme.domain.user.User;
import com.sjme.domain.candidate.dto.CandidateDto;
import com.sjme.mapper.InterviewEvaluationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjme.service.ICandidateInterviewerService;
import com.sjme.service.InterviewEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sjme.util.CommonUtil.convertIPage;

/**
 * <p>
 * 面试评价 服务实现类
 * </p>
 *
 * @author LY
 * @since 2025-11-02
 */
@Slf4j
@Service
public class InterviewEvaluationServiceImpl extends ServiceImpl<InterviewEvaluationMapper, InterviewEvaluation> implements InterviewEvaluationService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private CandidateClient candidateClient;
    @Autowired
    private ICandidateInterviewerService candidateInterviewerService;

    /**
     * 获取面试评价信息
     */
    @Override
    public IPage<InterviewEvaluationVo> queryInterviewEvaluation(InterviewEvaluationDto interviewEvaluation) {
        // 获取传参
        Integer current = interviewEvaluation.getCurrent() != null ? interviewEvaluation.getCurrent() : 1;
        Integer size = interviewEvaluation.getSize() != null ? interviewEvaluation.getSize() : 6;
        Integer evaluationStatus = interviewEvaluation.getEvaluationStatus();
        Integer result = interviewEvaluation.getResult();
        String candidateName = interviewEvaluation.getCandidateName();
        String interviewerName = interviewEvaluation.getInterviewerName();

        // 根据候选人和面试官名称构建查询条件
        // 1.根据面试官名称构建查询信息
        User userParam = new User();
        userParam.setName(interviewerName);
        List<User> users = userClient.queryUserByCondition(userParam);
        if (users.isEmpty()){
            return new Page<>(current, size);
        }
        // 1-1 面试官id列表
        List<String> interviewerIdList = users.stream().map(User::getId).collect(Collectors.toList());
        // 1-2 <面试官id,面试官>集合
        Map<String, User> interviewIdAndMap = users.stream().collect(Collectors.toMap(
                User::getId,
                user -> user
        ));

        // 2.根据传递的候选人名称限制候选人
        CandidateDto candidateParam = new CandidateDto();
        candidateParam.setName(candidateName);
        List<Candidate> candidateInfos = candidateClient.queryCandidateByCondition(candidateParam);
        if (candidateInfos.isEmpty()){
            return new Page<>(current, size);
        }
        // 2-1 候选人id列表
        List<String> candidateIdList = candidateInfos.stream().map(Candidate::getId).collect(Collectors.toList());
        // 2-2 <候选人id,候选人>集合
        Map<String, Candidate> candidateIdAndMap = candidateInfos.stream().collect(Collectors.toMap(
                Candidate::getId,
                candidate -> candidate
        ));

        // 3.根据候选人和面试官名称查关联表，获取面试评价id
        List<CandidateInterviewer> candidateInterviewerList = candidateInterviewerService.list(Wrappers.<CandidateInterviewer>lambdaQuery()
                .in(!candidateIdList.isEmpty(), CandidateInterviewer::getCandidateId, candidateIdList)
                .in(!interviewerIdList.isEmpty(), CandidateInterviewer::getInterviewerId, interviewerIdList));
        List<String> interviewEvaluationIdList = candidateInterviewerList.stream().map(CandidateInterviewer::getInterviewEvaluationId).collect(Collectors.toList());

        // 4.获取评价信息id 对应的候选人和面试官信息
        Map<String,User> evaluationIdAndInterviewerMap = new HashMap<>();
        Map<String,Candidate> evaluationIdAndCandidateMap = new HashMap<>();
        candidateInterviewerList.forEach(item -> {
            String interviewEvaluationId = item.getInterviewEvaluationId();
            String interviewerId = item.getInterviewerId();
            String candidateId = item.getCandidateId();
            // <评价信息id,候选人>
            evaluationIdAndCandidateMap.put(interviewEvaluationId,candidateIdAndMap.get(candidateId));
            // <评价信息id,面试官>
            evaluationIdAndInterviewerMap.put(interviewEvaluationId,interviewIdAndMap.get(interviewerId));
        });


        // 分页条件查询
        IPage<InterviewEvaluation> page = new Page<>(current, size);
        LambdaQueryWrapper<InterviewEvaluation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(evaluationStatus != null,InterviewEvaluation::getEvaluationStatus, evaluationStatus);
        wrapper.eq(result != null,InterviewEvaluation::getResult, result);
        wrapper.in(!interviewEvaluationIdList.isEmpty(), InterviewEvaluation::getId, interviewEvaluationIdList);
        this.page(page, wrapper);

        IPage<InterviewEvaluationVo> pageRes = convertIPage(page, InterviewEvaluationVo.class, item -> {
            String evaluationId = item.getId();
            InterviewEvaluationVo vo = new InterviewEvaluationVo();
            BeanUtils.copyProperties(item, vo);
            // 候选人信息
            Candidate candidate = evaluationIdAndCandidateMap.get(evaluationId);
            vo.setCandidateId(candidate.getId());
            vo.setCandidateName(candidate.getName());
            // 面试官信息
            User interviewer = evaluationIdAndInterviewerMap.get(evaluationId);
            vo.setInterviewerId(interviewer.getId());
            vo.setInterviewerName(interviewer.getName());
            return vo;
        });
        return pageRes;
    }
}
